import subprocess

from settings import colors
from utils import dict_to_sbar_args

_PLUGIN_DIR = "./plugins"


def init_spaces():
    # Spaces
    for sid in [1,2,3,"c","n","s"]:
        # name = spaces_icons[sid]
        space = dict(
            icon=dict(
                font="Hack Nerd Font:Bold:16.0",
                string=f"{sid}.",
                padding_left=8,
                padding_right=4,
                color=colors["white"],
                highlight_color=colors["red"],
            ),
            label=dict(
                padding_right=10,
                color="0xff7f8490",
                highlight_color="0xffffffff",
                font="sketchybar-app-font:Regular:16.0",
                y_offset=-1,
            ),
            background=dict(
                color="0xff363944",
                border_width=2,
                height=26,
                border_color="0xff414550",
                corner_radius=8,
            ),
            padding_right=1,
            padding_left=1,
            popup=dict(
                background=dict(
                    border_width=5,
                    border_color="0xff414550",
                ),
            ),
            script=f"{_PLUGIN_DIR}/space.sh",
            click_script=f"aerospace workspace {sid}",
        )
        subprocess.run(
            [
                "sketchybar",
                "-m",
                "--add",
                "item",
                f"space_{sid}",
                "left",
                "--set",
                f"space_{sid}",
                *dict_to_sbar_args(space),
                "--subscribe",
                f"space_{sid}",
                "front_app_switched",
            ]
        )

    front_app = dict(
        icon=dict(
            font="Hack Nerd Font:Bold:15.0",
            string="",
            padding_right=6,
        ),
        updates="when_shown",
        script=f"{_PLUGIN_DIR}/front_app.sh",
        background=dict(
            drawing=False,
        ),
    )
    subprocess.run(
        [
            "sketchybar",
            "-m",
            "--add",
            "item",
            "front_app",
            "left",
            "--set",
            "front_app",
            *dict_to_sbar_args(front_app),
            "--subscribe",
            "front_app",
            "front_app_switched",
        ]
    )


def init_cpu():
    cpu = dict(
        # position="right",
        icon=dict(
            font="Hack Nerd Font:Bold:15.0",
            string="",
        ),
        graph=dict(color="0xfffc5d7c"),
        background=dict(
            height=22,
            color=dict(alpha=0),
            border_color=dict(alpha=0),
            corner_radius=9,
            drawing=True,
        ),
        label=dict(
            string="cpu ??%",
            font=dict(
                family="SF Mono",
                style="Bold",
                size=10,
            ),
            align="right",
            padding_right=0,
            width=0,
            y_offset=4,
        ),
        padding_right=9,
        update_freq=2,
        script=f"{_PLUGIN_DIR}/cpu_graph.sh",
        click_script="open -a 'Activity Monitor'",
    )
    subprocess.run(
        [
            "sketchybar",
            "--add",
            "graph",
            "cpu",
            "right",
            "42",
            "--set",
            "cpu",
            *dict_to_sbar_args(cpu),
        ]
    )


def init_items():
    init_spaces()
    # init_cpu()
